<!--策略启动-AI-->
<template>
  <div class="all">
    
    <scroller class="scroller">
      <div class="wrapper">

        <!--服务器-->
        <VServer v-if="api && api.apiId" class="server" ref="server"></VServer>

        <!--API-->
        <VApi class="api" ref="api" v-model="api" :noExchanged="noExchanged" @apiChange="apiChange"></VApi>
        
        <div v-if="pairMap">
          <!--交易对-->
          <VPair class="pair" ref="pair" :pairMap="pairMap" @pairClick="pairClick"></VPair>

          <!--预期年化收益率-->
          <VYear class="year" v-if="pairMap.yearIncome && pairMap.yearIncome.length" :yearIncome="pairMap.yearIncome"></VYear>
          
          <!--投资方向-->
          <VTarget class="target" v-if="isTarget" :target="target" :counterCoin="pairMap.counterCoin" :baseCoin="pairMap.baseCoin" @click="targetClick"></VTarget>
          
          <!--策略类型-->
          <VType class="type" v-if="tagList && tagList.length" v-model="tag" :tagList="tagList"></VType>

          <!--投入资金-->
          <div class="line bg3" v-if="reserveOpen"></div>
          <VInputUp class="input" ref="input" v-model="investCoinnNum" 
          :type="'ai'" :name="strategyName" :api="api" :investCoin="investCoin" :pairMap="pairMap" :limitMap="limitMap"></VInputUp>
          
          <!--其他设置-->
          <VSet class="set" ref="set" v-model="sets" @open="setOpen"></VSet>
        </div>
      </div>
    </scroller>

    <!--启动-->
    <div class="up bg3">
      <div class="up-but main1" @click="upClick">
        <text class="font32 text1">{{T('启动')}}</text>
      </div>
    </div>
  </div>
</template>

<script>

//组件
import VServer from "@/up/view/VServer.vue";
import VApi from "@/up/view/VApi.vue";
import VPair from "@/up/view/VPair.vue";
import VYear from "@/up/view/VYear.vue";
import VTarget from "@/up/view/VTarget.vue";
import VType from "@/up/view/VType.vue";
import VInputUp from "@/up/view/VInputUp.vue";
import VSet from "@/up/view/VSet.vue";

//功能
import modal from "@/_js/modal";
import math from "@/_js/math";
import config from "@/_js/config";
import navigator from "@/_js/navigator"; 
import broadcast from "@/_js/broadcast";
import mUp from "@/up/amUp";
import debug from "@/_js/debug";


import weexHttp from "@/_js/https";
const weexFunction = weex.requireModule('WeexFunction');
const dom = weex.requireModule('dom')

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  components: {
    VServer,
    VApi,
    VPair,
    VYear,
    VTarget,
    VType,
    VInputUp,
    VSet,
  },

  props:{
    isTarget:{ //turn显示投资方向
      type:Boolean,
      default:false,
    },

    strategyId:String, //策略ID
    strategyName:String, //策略名称
    noExchanged:{ //不可选交易所对象{name}
      type:Object,
      default:{'uniswap':'Uniswap只支持马丁追踪自定义启动'},
    }, 
  },
  
  data() {
    return {
      T:T,
      config:config,
      math:math,
      isUp:false, //turn已经启动过合约

      limitMap:undefined, // 投入限制
      api:undefined, //当前选中API
      pairMap:undefined, //当前选中交易对

      tag:0, //当前选中类型
      tagList:undefined, //类型列表

      target:false, //turn赚交易币 false赚本币
      investCoin:undefined, //投资币
      investCoinnNum:'', //投资数量

      sets:undefined, //其他设置
    };
  },

  mounted() {
  },

  methods: {

    //初始化数据
    initData(counterCoin, baseCoin) {

      this.tag = 0; //默认选中策略类型
      this.investCoin = this.target ? counterCoin : baseCoin;  //投资币
      this.sets = this.sets ? {} : undefined; //其他设置
    },

    /*************************************************通信触发*/
    //完全显示
    viewappear() {
      this.$refs.pair && this.$refs.pair.viewappear();
    },

    //完全隐藏
    viewdisappear() {
      this.$refs.pair && this.$refs.pair.viewdisappear();
    },

    /********************************************** https*/
    //http策略-AI
    httpStrategyAi(target, counterCoin, baseCoin){

      modal.showLoad();
      var param = {'strategyId':this.strategyId,
                   'exchangeId':this.api.exchangeId, 
                   'exchangeName':this.api.exchangeName, 
                   'counterCoin':counterCoin, 
                   'baseCoin':baseCoin,
                   'investType':target};

      weexHttp.post("strategyV3/aiStartPage",param,true,(data)=>{
        modal.hideLoad();
        if(data.status == 200){
          data = data.data;
          var pairMap = data.pairMap;
          pairMap['baseCoinUsdPrice'] =  math.chu(pairMap.usdPrice, pairMap.last, math.getNumber(pairMap.baseCoinDecimal));
          this.target = target; //策略方向

          //交易对改变：保存API默认配置
          if (counterCoin != pairMap.counterCoin || baseCoin != pairMap.baseCoin) {
            counterCoin = pairMap.counterCoin;
            baseCoin = pairMap.baseCoin;
          }
          this.$refs.api.setDefaultApi(this.api.apiId, counterCoin, baseCoin);

          this.limitMap =  data.limitMap;
          this.initData(counterCoin, baseCoin); //初始化数据
          this.tagList = data.tagList; //策略类型数组
          this.pairMap = pairMap; //交易对

          this.httpCheckContract(); //查询权限
        } else {
          modal.toast(data.msg);
        }
      });
    },

    //http策略-启动过合约策略
    httpCheckContract(){
      if (this.api.apiId == '') {
        this.isUp = false;
        return;
      }

      var param = {'apiId':this.api.apiId,  
                   'exchangeName':this.api.exchangeName};
      weexHttp.post("contract/querySpotPermission",param,true,(data)=>{
        if(data.status == 200){
          var data = data.data;
          this.isUp = data.isStartStrategy;
          if (this.isUp) {
            this.tipeUpCoin();
          }
        } else {            
          this.isUp = false;
        }
      });
    },
    
    /********************************** 其他*/
    //提示启动过该币种
    tipeUpCoin(){
      var tipe = T('单个统一账户只支持一个合约策略且执行合约策略时不可执行币币策略，&{1}账户已有执行中的合约策略，您可前往执行列表查看。');
      tipe = tipe.replace("&{1}", this.api.apiName);
      modal.alert(tipe,()=>{
      });
    },
    
    /********************************** 改变*/
    //API改变
    apiChange(counterCoin, baseCoin){

      counterCoin = counterCoin ? counterCoin : this.pairMap.counterCoin;
      baseCoin = baseCoin ? baseCoin : this.pairMap.baseCoin;
      this.httpStrategyAi(this.target, counterCoin, baseCoin);
    },

    /********************************** 点击*/
    //交易对
    pairClick(){
      
      var param = {exchangeId:this.api.exchangeId,
                  strategyId:this.strategyId,
                  exchangeName:this.api.exchangeName,
                  counterCoin:this.pairMap.counterCoin,
                  baseCoin:this.pairMap.baseCoin};
      navigator.open('', 'up/SelectPairAi', param);

      var self = this;
      broadcast.subscribeOnce('SelectPairAi', (pair)=>{
        
        if (pair.counterCoin == self.pairMap.counterCoin && 
            pair.baseCoin == self.pairMap.baseCoin) {
          return;
        }
        self.httpStrategyAi(this.target, pair.counterCoin, pair.baseCoin);
      })
    },

    //策略方向
    targetClick(target) {
      this.httpStrategyAi(target, this.pairMap.counterCoin, this.pairMap.baseCoin);
    },

    //其他设置-展开
    setOpen() {
      setTimeout(() => {
        this.$refs.set && dom.scrollToElement(this.$refs.set, {offset:0});
      }, 100);
    },

    //启动
    upClick() {
      
      //启动过OkexV5合约
      if (this.isUp) {
        this.tipeUpCoin();
        return;
      }

       //服务器检查
      if (this.$refs.server && this.$refs.server.checkError()) {
        return;
      }
      
      //投入资金-测试
      if (this.$refs.input.checkError()) {
        return;
      }
      
      //其他设置
      if (this.$refs.set.checkError()) {
        return;
      }
      
      modal.showLoad();

      var setMap = this.$refs.set.getParam();
      var bannerId = this.tagList[this.tag].bannerId;

      
      var address = '';
      if (this.$refs.server) {
        var server =  this.$refs.server.server;
        address = server ? server.innerIp : '';
      }

      var tradeFee = isNaN(this.api.tradeFee) ? '0' : this.api.tradeFee;
      var param = {'address':address,
                  'api':this.api, 'buyFee':tradeFee,
                  'strategyId':this.strategyId, 
                  
                  'exchangeId':this.api.exchangeId, 
                  'counterCoinId':this.pairMap.counterCoinId, 
                  'baseCoinId':this.pairMap.baseCoinId,

                  'investCoin':this.investCoin, 'investCoinnNum':this.investCoinnNum,
                  'investType':this.target, 'bannerId':bannerId,
                  'setMap':setMap};
      mUp.httpUpAi(param, ()=>{
          broadcast.send(broadcast.EXECUTE_UPDATE);
          navigator.back();
      });
    },
  }
}
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.scroller {
  flex: 1;
}
.wrapper{
  padding-left: 40px;
  padding-right: 40px;
}
.line{
  height: 2px;
  margin-bottom: 40px;
}

.server {
  margin-top: 40px;
}

.api {
  margin: 40px;
}

.pair {
  margin-bottom: 40px;
}

.year {
  margin-bottom: 40px;
}

.target {
  margin-bottom: 40px;
}

.type {
  margin-bottom: 40px;
}

.input {
  margin-bottom: 40px;
}

.set {
  margin-bottom: 40px;
}

/*************** 启动*/
.up{
  flex-direction: column;
  height: 164px;

  padding-left: 40px;
  padding-right: 40px;
}
.up-but{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 80px;
  border-radius: 4px;

  margin-top: 28px;
}
</style>